import React from 'react';
import PropTypes from 'prop-types';
import Edit from './edit'
import { stripesConnect } from '@folio/stripes/core';
import { injectIntl,FormattedMessage } from 'react-intl';
class EditFormSachet extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            operator: {},
            editParams:{},
            initParam:{}
        };
    }
    static manifest = Object.freeze({
       
        id: { initialValue: '' },
        recordId:{initialValue:''},
        httpBlackListInvoke: {
          type: 'okapi',
          accumulate: true,
          path: () => 'party/reader/credit/record'
        },
        httpBlackListModifyInvoke: {
          type: 'okapi',
          accumulate: true,
          pk:'sdsi',
          path: 'party/reader/credit/record/modify/%{id}/and/%{recordId}',
          PUT:{
            path: 'party/reader/credit/record/modify/%{id}/and/%{recordId}',
         
          }
        }
      });
    static propTypes = {
        mutator: PropTypes.shape({
            httpBlackListInvoke: PropTypes.shape({
            PUT: PropTypes.func.isRequired,
            POST: PropTypes.func.isRequired,
            GET: PropTypes.func.isRequired,
            DELETE: PropTypes.func.isRequired
          }) ,
          httpBlackListModifyInvoke: PropTypes.shape({
            PUT: PropTypes.func.isRequired,
            POST: PropTypes.func.isRequired,
            GET: PropTypes.func.isRequired,
            DELETE: PropTypes.func.isRequired
          }) 
        }).isRequired,
        name:PropTypes.string,
        okapi:PropTypes.object,
        params:PropTypes.object,
        removeTopMenu:PropTypes.func,
        matchPath:PropTypes.string,
        toModel:PropTypes.func
      }; 
    componentDidMount(){
        this.initData();
        // console.log(this.props);
        const { currentUser } = this.props.okapi;
        if (currentUser) {
          this.setState({
            operator: currentUser
          });
        }
    }
    initData = () => {
        const {mutator}=this.props
        if (
    
          this.props.params &&
          this.props.params.id &&
          this.props.params.recordId
        ) {
          const params = this.props.params;
       
          const { GET } = this.props.mutator.httpBlackListModifyInvoke;
          mutator.recordId.replace(this.props.params.recordId);
          mutator.id.replace( this.props.params.id);
         
          GET().then(res=>{
            const list = res.punishmentRecordGroup;
       
            if(list.length === 0){
                alert("获取黑名单数据失败了");
                this.cancel(false);
                return;
            }
            let editParams= undefined
            list.forEach(item=>{
                if(item.id === this.props.params.recordId){
                    editParams = item;
                    editParams.barcode = res.readerInfo.barcode
                    return;
                }
            })
            if(!editParams){
                alert("获取黑名单数据失败了");
                this.cancel(false);
                return;
            }
            this.setState({
                initParam: res,
                editParams
            });
          })
      
         }
      };
       cancel = (flag = true) => {
        const { removeTopMenu, matchPath } = this.props;
        removeTopMenu(matchPath, flag);
      };
        editCommit = ({ requestPreferences, creds, ...formData }) => {
          let readerRecords=this.state.initParam;
          let punishmentRecordGroup = []
             delete formData.barcode
          readerRecords.operator = this.state.operator;
          delete readerRecords.operator.addresses;
          delete readerRecords.operator.preferredContactTypeId;
          delete readerRecords.operator.servicePoints;
          delete readerRecords.operator.curServicePoint;
          delete readerRecords.operator.dateOfBirth;
          delete readerRecords.metadata;
          punishmentRecordGroup.push({...formData})
          readerRecords.punishmentRecordGroup = punishmentRecordGroup;
          //console.log(readerRecords)
          this.props.mutator.httpBlackListModifyInvoke.PUT(readerRecords ).then(res=>{
            alert("修改成功!");
            this.cancel(false);
          });;
     

      }
    render() {
        return (
            <Edit 
            onSubmit={this.editCommit}
            initialValues={this.state.editParams}
            cancel={this.cancel}
            exitFlag={this.state.exitFlag}
            httpValidatorInvoke={this.props.mutator.httpBlackListModifyInvoke}
            toModel={this.props.toModel}
            removeTopMenu={this.props.removeTopMenu}
            stripes={this.props.stripes}
            form={this.props.name}
            />
                
          
        );
    }
}

 
export default injectIntl(stripesConnect(EditFormSachet));
 
